home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE26 / EX1.C next >
C/C++ Source or Header  |  1995-05-29  |  3KB  |  80 lines

  1. #include <genstub.c>
  2.  
  3. #define OUR_EXCEPTION  0xE0000000
  4.  
  5. DWORD TestException(DWORD dwExceptionCodeReceived,
  6.                     LPEXCEPTION_POINTERS lpExceptionInfo, LPCONTEXT lpContext,
  7.                     LPEXCEPTION_RECORD lpExceptionRecord, DWORD dwTestException )
  8. {
  9.     *lpContext = *lpExceptionInfo->ContextRecord;
  10.     *lpExceptionRecord = *lpExceptionInfo->ExceptionRecord;
  11.  
  12.     if ( dwExceptionCodeReceived == dwTestException )
  13.         return EXCEPTION_EXECUTE_HANDLER;
  14.     else
  15.         return EXCEPTION_CONTINUE_SEARCH;
  16. }
  17.  
  18. LPARAM CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  19. {
  20.    switch (uMsg)
  21.    {
  22.       case WM_DESTROY:
  23.          PostQuitMessage(0);
  24.       break;
  25.       case WM_COMMAND:
  26.       {
  27.          switch (wParam)
  28.          {
  29.              case IDM_TEST:
  30.              {
  31.                 char szBuffer[128];
  32.                 LPCONTEXT lpContext = (LPCONTEXT)
  33.                         HeapAlloc(GetProcessHeap(), 0, sizeof(CONTEXT));
  34.                 LPEXCEPTION_RECORD lpExcptRec = (LPEXCEPTION_RECORD)
  35.                         HeapAlloc(GetProcessHeap(), 0, sizeof(EXCEPTION_RECORD));
  36.  
  37.                 // Raise exception try block.
  38.                 try
  39.                 {
  40.                    RaiseException(OUR_EXCEPTION, 0, 0, NULL);
  41.                 }
  42.                 except( TestException(GetExceptionCode(), GetExceptionInformation(),
  43.                         lpContext, lpExcptRec, OUR_EXCEPTION ) )
  44.                 {
  45.                    wsprintf( szBuffer, "Our Exception, %x, trapped at %X:%X",
  46.                              lpExcptRec->ExceptionCode, lpContext->SegCs,
  47.                              lpContext->Eip );
  48.                    MessageBox( hWnd, szBuffer, "Exception Handler", MB_OK );
  49.                 }
  50.  
  51.                 try
  52.                 {
  53.                    int i = 1;
  54.                    i = i / 0;
  55.                 }
  56.                 finally
  57.                 {
  58.                    wsprintf( szBuffer, "Abnormal Termination: %d",
  59.                              AbnormalTermination() );
  60.                    MessageBox( hWnd, szBuffer, "Finally Handler", MB_OK );
  61.                 }
  62.                 HeapFree( GetProcessHeap(), NULL, lpExcptRec );
  63.                 HeapFree( GetProcessHeap(), NULL, lpContext );
  64.  
  65.              }
  66.              break;
  67.  
  68.              case IDM_EXIT: {
  69.                  DestroyWindow(hWnd);
  70.              }
  71.              break;
  72.          }
  73.       }
  74.       break;
  75.  
  76.       default:
  77.           return (DefWindowProc(hWnd, uMsg, wParam, lParam));
  78.    }
  79.    return (NULL);
  80. }